package com.ly.dao;

import com.ly.entity.BankAccount;
import com.ly.utils.IOUtil;

import java.util.ArrayList;
import java.util.List;

import static com.ly.constants.Constant.ACCOUNT_FILE_NAME;
import static com.ly.utils.IOUtil.readAccountsFromFile;

/**
 * Author: LiYao
 * CreateTime: 2025-03-11 19:35
 * Description:
 * Version: 1.0
 */
public class AccountDao {

    public ArrayList<BankAccount> getAccountList(){
        ArrayList<BankAccount> list  = readAccountsFromFile(ACCOUNT_FILE_NAME);
        return  list;
    }


    public void updateBalance(BankAccount account, double balance){
        ArrayList<BankAccount> list = getAccountList();
        for (BankAccount bankAccount : list) {
            if (bankAccount.getAccountID().equals(account.getAccountID())){
                bankAccount.setBalance(balance);
            }

        }
        IOUtil.writeAccountsToFile(list,ACCOUNT_FILE_NAME);
    }


}
